package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.sync.SyncPsnInfoEntity;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.config.SyncConfig;
import com.pactera.madp.cp.context.SyncStateContext;
import com.pactera.madp.cp.mapper.sync.SyncPsnInfoMapper;
import com.pactera.madp.cp.service.sync.SyncPsnInfoHisService;
import com.pactera.madp.cp.service.sync.SyncPsnInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

@Slf4j
@Service
@AllArgsConstructor
public class SyncPsnInfoServiceImpl extends ServiceImpl<SyncPsnInfoMapper, SyncPsnInfoEntity> implements SyncPsnInfoService {

    private final SyncPsnInfoHisService syncPsnInfoHisService;
    private final SyncConfig syncConfig;

    @Override
    public void addYesterdayToHis() {
        log.debug("人员员工信息：addYesterdayToHis");
        String date = DateUtil.format(new Date(), Constants.DATE_FORMAT);
        syncPsnInfoHisService.syncOneDay(date);
    }

    @Override
    public void deleteYesterday() {
        log.debug("人员员工信息：deleteYesterday");
        this.remove(Wrappers.emptyWrapper());
    }

    @Override
    public void parse() {
        log.debug("人员员工信息：parse");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -1);
        String fileDate = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        String filepath = SyncUtils.buildFilePath(syncConfig.getHrPath(), Constants.FN_PSN, fileDate);
        File newestFile = SyncUtils.getNewestFile(filepath);

        List<SyncPsnInfoEntity> entityList
                = SyncUtils.parse(newestFile, Constants.SEPARATOR_STX, SyncPsnInfoEntity.class, syncConfig.getHrCharset());
        log.debug("人员文件解析总数：" + entityList.size());

        entityList = specialHandle(entityList);

        this.saveBatch(entityList);
    }

    @Override
    public void deleteHisOneDay() {
        log.debug("人员员工信息：deleteHisOneDay");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -30);
        String date = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        syncPsnInfoHisService.deleteOneDate(date);
    }

    @Override
    public void syncBusiness() {
        log.debug("人员员工信息：syncBusiness");
        SyncStateContext.get().setFinalState(true);
    }

    // 人员机构变更处理
    private List<SyncPsnInfoEntity> specialHandle(List<SyncPsnInfoEntity> psnList) {
        List<SyncPsnInfoEntity> newPsnList = new ArrayList<>();
        // 获取排除机构id列表
        Set<String> excludeOrg = syncConfig.getExcludeOrg();

        // 移除分行原有人员和重置人员机构
        psnList.forEach(p -> {
            // 过滤掉湘乡市村镇银行人员
            if (StrUtil.equals("84633348001636140ed59a4511e1a5ca", p.getUnit())) {
                return;
            }
            // 重置总行本部人员机构
            else if (StrUtil.equals("4000", p.getUnit())) {
                p.setUnit("101");
            }
            // 重置支行人员机构
            else if (!excludeOrg.contains(p.getFstDept())) {
                p.setUnit(p.getFstDept());
            }
            newPsnList.add(p);
        });

        return newPsnList;
    }

}